<?php

require_once './config.php';

$gid = $_REQUEST['gid'];
$username = $_REQUEST['username'];
$img = $_REQUEST['img'];
$gname = $_REQUEST['gname'];
$price = $_REQUEST['price'];
$count = $_REQUEST['count'];

$sql = "SELECT * FROM cartinfo WHERE gid = $gid AND username = '$username'";

$result = mysqli_query($conn, $sql);

$data = mysqli_fetch_assoc($result);

if ($data) {
    $count = $data['count'] + $count;
    $id = $data['id'];

    // echo $count;

    $sql = "UPDATE cartinfo SET count = $count WHERE id = $id";

    mysqli_query($conn, $sql);

    $rows = mysqli_affected_rows($conn);

} else {
    $sql = "INSERT INTO cartinfo(gid, username, img, gname, price, count) VALUES ($gid, '$username', '$img', '$gname', $price, $count)";

    mysqli_query($conn, $sql);

    $rows = mysqli_affected_rows($conn);
}

if ($rows > 0) {
    echo json_encode(array(
        'code' => 1,
        'msg' => '成功',
    ));
}
